package com.chanlib.widget;


import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chanlib.R;
import com.chanlib.util.RecyclerViewUtil;

public class TitleBar extends LinearLayout implements View.OnClickListener {
    private LinearLayout mRootView; //增加rootView适应沉浸状态栏
    private LinearLayout mLeftView;
    private LinearLayout mMiddleView;
    private LinearLayout mRightView;

    private TextView mTitleTextView;
    private TextView mRightTextView;
    private TextView mLeftTextView;
    private ImageView mLeftImageView;
    private ImageView mRightFirstImageView;
    private ImageView mRightImageView;

    private OnClickListener mLeftImageClickListener;
    private OnClickListener mRightImageClickListener;
    private OnClickListener mRightFirstImageClickListener;
    private OnClickListener mLeftTextClickListener;
    private OnClickListener mRightTextClickListener;

    private int mTitleTextSize = 16;   //标题文字大小
    private int mMenuTextSize = 14;   //菜单文字大小
    private int mTextColor = Color.WHITE;

    private boolean mTitleFixedCenter = true;  //标题是否居中

    /**
     * 双击滑动到顶部的参数
     */
    private static final long DOUBLE_CLICK_TIME_DELTA = 300;//milliseconds
    private long lastClickTime = 0;
    private View mSmoothToTopView;

    public TitleBar(Context context) {
        super(context);
        init(context);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        mRootView = new LinearLayout(context);

        mRootView.setGravity(Gravity.CENTER);
        mRootView.setOrientation(HORIZONTAL);

        mLeftView = new LinearLayout(context);
        mLeftView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mLeftView.setGravity(Gravity.CENTER_VERTICAL);

        mMiddleView = new LinearLayout(context);
        mMiddleView.setLayoutParams(new LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
        mMiddleView.setGravity(Gravity.CENTER);

        mRightView = new LinearLayout(context);
        mRightView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        mRightView.setGravity(Gravity.CENTER_HORIZONTAL);

        mTitleTextView = createTextView(mTitleTextSize);
        mTitleTextView.setSingleLine(true);
        mTitleTextView.setMaxEms(10);
        mTitleTextView.setEllipsize(TextUtils.TruncateAt.END);
        TextPaint paint = mTitleTextView.getPaint();
        paint.setFakeBoldText(true);
        mMiddleView.addView(mTitleTextView, 0);

        mMiddleView.setOnClickListener(this);
        int margin = getResources().getDimensionPixelOffset(R.dimen.margin_side);
        mLeftView.setPadding(margin, 0, margin, 0);
        mRightView.setPadding(margin, 0, margin, 0);
        mRootView.addView(mLeftView);
        mRootView.addView(mMiddleView);
        mRootView.addView(mRightView);


        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, getResources().getDimensionPixelOffset(R.dimen.title_bar_height));
//        params.setMargins(margin, 0, margin, 0);
        addView(mRootView, params);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        updateTitleContainer();
    }

    //中间文字偏移一定位置保证居中
    private void updateTitleContainer() {
        int leftPadding = 0;
        int rightPadding = 0;
        if (mTitleFixedCenter) {
            if (mLeftView.getVisibility() == VISIBLE && mRightView.getVisibility() == VISIBLE) {
                if (mLeftView.getMeasuredWidth() >= mRightView.getMeasuredWidth()) {
                    leftPadding = 0;
                    rightPadding = mLeftView.getMeasuredWidth() - mRightView.getMeasuredWidth();
                } else {
                    leftPadding = mRightView.getMeasuredWidth() - mLeftView.getMeasuredWidth();
                    rightPadding = 0;
                }
            } else if (mLeftView.getVisibility() == VISIBLE) {
                leftPadding = 0;
                rightPadding = mLeftView.getMeasuredWidth();
            } else if (mRightView.getVisibility() == VISIBLE) {
                leftPadding = mRightView.getMeasuredWidth();
                rightPadding = 0;
            }
        }
        if (leftPadding != mMiddleView.getPaddingLeft() || rightPadding != mMiddleView.getPaddingRight()) {
            mMiddleView.setPadding(leftPadding, 0, rightPadding, 0);
        }
    }

    private ImageView createImageView() {
        ImageView imageView = new ImageView(getContext());
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//        int padding = (int) getResources().getDimension(R.dimen.image_button_padding);
//        imageView.setPadding(padding, padding, padding, padding); //统一增加12个像素
        return imageView;
    }

    private TextView createTextView(float size) {
        TextView textView = new TextView(getContext());
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
        textView.setTextColor(mTextColor);
        return textView;
    }

    public void setTitle(CharSequence text) {
        mTitleTextView.setText(text);
    }

    public void setTitle(@StringRes int resId) {
        setTitle(getResources().getString(resId));
    }

    public void setTitleSize(int size) {
        mTitleTextSize = size;
        mTitleTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTitleTextSize);
    }

    public void setTitleColor(@ColorInt int color) {
        mTextColor = color;
        mTitleTextView.setTextColor(color);
    }

    public void setLeftImageResource(@DrawableRes int resId) {
        if (mLeftImageView == null) {
            mLeftImageView = createImageView();
            mLeftView.setOnClickListener(mLeftImageClickListener);//
            mLeftView.addView(mLeftImageView);
        }
        mLeftView.setVisibility(VISIBLE);
        mLeftImageView.setVisibility(VISIBLE);
        mLeftImageView.setImageResource(resId);
    }

    public void setRightImageResource(@DrawableRes int resId) {
        if (mRightImageView == null) {
            mRightImageView = createImageView();
            mRightImageView.setOnClickListener(mRightImageClickListener);
            mRightView.addView(mRightImageView);
        }
        mRightView.setVisibility(VISIBLE);
        mRightImageView.setVisibility(VISIBLE);
        mRightImageView.setImageResource(resId);
        changeRightFirstImageMargin();
    }

    /**
     * titlebar添加右边的textview
     *
     * @param text
     */
    public void addRightTextView(String text) {
        if (mRightTextView == null) {
            mRightTextView = createTextView(mMenuTextSize);
            mRightTextView.setOnClickListener(mRightTextClickListener);
            mRightView.addView(mRightTextView);
        }
        mRightView.setVisibility(VISIBLE);
        mRightTextView.setVisibility(VISIBLE);
        mRightTextView.setText(text);
    }

    public void addLeftTextView(String text) {
        if (mLeftTextView == null) {
            mLeftTextView = createTextView(mMenuTextSize);
            mLeftTextView.setOnClickListener(mLeftTextClickListener);
            mLeftView.addView(mLeftTextView);
        }
        mLeftView.setVisibility(VISIBLE);
        mLeftTextView.setVisibility(VISIBLE);
        mLeftTextView.setText(text);
    }

    public void setRightFirstImageResource(@DrawableRes int resId) {
        if (mRightFirstImageView == null) {
            mRightFirstImageView = createImageView();
            mRightFirstImageView.setOnClickListener(mRightFirstImageClickListener);
            mRightView.addView(mRightFirstImageView, 0);
        }
        mRightView.setVisibility(VISIBLE);
        mRightFirstImageView.setVisibility(VISIBLE);
        mRightFirstImageView.setImageResource(resId);
        changeRightFirstImageMargin();
    }

    private void changeRightFirstImageMargin() {
        if (mRightFirstImageView != null && mRightFirstImageView.getVisibility() == VISIBLE) {
            LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            int marginRight;
            if (mRightImageView.getVisibility() == VISIBLE) {
                marginRight = getResources().getDimensionPixelOffset(R.dimen.margin_title_bar_space);
            } else {
                marginRight = 0;
            }
            params.setMargins(0, 0, marginRight, 0);
            mRightFirstImageView.setLayoutParams(params);
        }
    }

    public void setLeftImageOnClickListener(OnClickListener listener) {
        mLeftImageClickListener = listener;
        if (mLeftImageView != null) {
            mLeftImageView.setOnClickListener(listener);
        }
    }

    public void setRightImageOnClickListener(OnClickListener listener) {
        mRightImageClickListener = listener;
        if (mRightImageView != null) {
            mRightImageView.setOnClickListener(listener);
        }
    }

    public void setRightFirstImageOnClickListener(OnClickListener listener) {
        mRightFirstImageClickListener = listener;
        if (mRightFirstImageView != null) {
            mRightFirstImageView.setOnClickListener(listener);
        }
    }

    public void setLeftTextOnClickListener(OnClickListener listener) {
        mLeftTextClickListener = listener;
        if (mLeftTextView != null) {
            mLeftTextView.setOnClickListener(listener);
        }
    }

    public void setRightTextOnClickListener(OnClickListener listener) {
        mRightTextClickListener = listener;
        if (mRightTextView != null) {
            mRightTextView.setOnClickListener(listener);
        }
    }

    public ViewGroup getLeftView() {
        return mLeftView;
    }

    public ViewGroup getMiddleView() {
        return mMiddleView;
    }

    public void addTitleTextView() {
        mMiddleView.addView(mTitleTextView, 0);
    }

    public ViewGroup getRightView() {
        return mRightView;
    }

    public void setLeftViewVisibility(boolean visible) {
        mLeftView.setVisibility(visible ? VISIBLE : GONE);
        setLeftImageVisibility(visible);
    }

    public void setRightViewVisibility(boolean visible) {
        mRightView.setVisibility(visible ? VISIBLE : GONE);
        setRightImageVisibility(visible);
    }

    public void setLeftImageVisibility(boolean visible) {
        if (mLeftImageView != null) {
            mLeftImageView.setVisibility(visible ? VISIBLE : GONE);
        }
    }

    public void setRightImageVisibility(boolean visible) {
        if (mRightImageView != null) {
            mRightImageView.setVisibility(visible ? VISIBLE : GONE);
        }
    }

    public void setRightFirstImageVisibility(boolean visible) {
        if (mRightFirstImageView != null) {
            mRightFirstImageView.setVisibility(visible ? VISIBLE : GONE);
        }
    }

    public void setLeftTextVisibility(boolean visible) {
        if (mLeftTextView != null) {
            mLeftTextView.setVisibility(visible ? VISIBLE : GONE);
        }
    }

    public void setRightTextVisibility(boolean visible) {
        if (mRightTextView != null) {
            mRightTextView.setVisibility(visible ? VISIBLE : GONE);
        }
    }

    /**
     * 双击操作，目前只对WebView,ScrollView,AbsListView做操作
     */
    private void doubleClick() {
        RecyclerViewUtil.smoothScrollToTop(mSmoothToTopView);
    }

    /**
     * 设置可滑动的View
     *
     * @param smoothToTopView
     */
    public void setSmoothToTopView(View smoothToTopView) {
        mSmoothToTopView = smoothToTopView;
    }

    public ImageView getLeftImageView() {
        return mLeftImageView;
    }

    public ImageView getRightImageView() {
        return mRightImageView;
    }

    @Override
    public void onClick(View v) {
        long clickTime = System.currentTimeMillis();
        if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA) {
            /**双击在一定间隔之内触发方法**/
            doubleClick();
            lastClickTime = 0;
        } else {
            lastClickTime = clickTime;
        }
    }
}
